using NUnit.Framework;
using Rhino.Mocks;
using ShadeTree.Binding.Lists;

namespace ShadeTree.Binding.Tests.Lists
{
    [TestFixture]
    public class ListConfigurationTester
    {
        #region Setup/Teardown

        [SetUp]
        public void SetUp()
        {
            ListRepository.ClearLists();
            ListRepository.ClearListSources();
        }

        #endregion

        [Test]
        public void Fetch_a_Picklist_by_key_from_the_ListSource_If_it_does_not_already_exist()
        {
            var mocks = new MockRepository();
            var source = mocks.StrictMock<IListSource>();
            ListRepository.RegisterListSource(source);

            IPicklist list1 = new Picklist<string>("a");
            using (mocks.Record())
            {
                Expect.Call(source.GetList("1")).Return(list1);
            }

            using (mocks.Playback())
            {
                ListRepository.GetList("1").ShouldEqual(list1);
                ListRepository.GetList("1").ShouldEqual(list1);
            }
        }

        [Test]
        public void Fetch_a_Picklist_by_type_from_the_ListSource_If_it_does_not_already_exist()
        {
            var mocks = new MockRepository();
            var source = mocks.StrictMock<IListSource>();
            ListRepository.RegisterListSource(source);

            IPicklist list1 = new Picklist<string>("a");
            using (mocks.Record())
            {
                Expect.Call(source.GetList<string>()).Return(list1);
            }

            ListRepository.GetList<string>().ShouldEqual(list1);
            ListRepository.GetList<string>().ShouldEqual(list1);
        }

        [Test]
        public void Fetch_a_stored_picklist_by_key_from_ListRepsitory()
        {
            IPicklist list1 = new Picklist<string>("a");
            IPicklist list2 = new Picklist<string>("b");
            IPicklist list3 = new Picklist<string>("c");

            ListRepository.SetList("1", list1);
            ListRepository.SetList("2", list2);
            ListRepository.SetList("3", list3);

            ListRepository.GetList("1").ShouldEqual(list1);
            ListRepository.GetList("2").ShouldEqual(list2);
            ListRepository.GetList("3").ShouldEqual(list3);
        }

        [Test]
        public void Fetch_a_stored_picklist_by_type_from_ListRepsitory()
        {
            IPicklist list1 = new Picklist<string>("a");
            IPicklist list2 = new Picklist<string>("b");
            IPicklist list3 = new Picklist<string>("c");

            ListRepository.SetList<double>(list1);
            ListRepository.SetList<bool>(list2);
            ListRepository.SetList<string>(list3);

            ListRepository.GetList<double>().ShouldEqual(list1);
            ListRepository.GetList<bool>().ShouldEqual(list2);
            ListRepository.GetList<string>().ShouldEqual(list3);
        }
    }
}